<template>
  <div class="login-container">
    <div class="wrap">
      <header>
        <em></em>
        <span>综合查询子系统</span>
      </header>
      <article>
        <section>
          <aside>
            <em>
              <img src="../images/login_sys/user.png">
            </em>

            <el-form autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left" label-width="0px"
                     class="card-box login-form">
              <el-form-item prop="username" class="name">
                <i :class="username_i"></i>
                <el-input name="email" type="text" v-model="loginForm.username" autoComplete="on" placeholder="用户名" @blur="usernameBlur" @focus="usernameFocus"></el-input>
              </el-form-item>
              <el-form-item prop="password" class="password">
                <i :class="password_i"></i>
                <el-input name="password" type="password" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="on"
                          placeholder="密码" @blur="passwordBlur" @focus="passwordFocus"></el-input>
              </el-form-item>
              <el-form-item class="login-btn">
                <el-button type="primary" style="width:100%;" :loading="loading" @click.native.prevent="handleLogin" @keyup.enter.native="handleLogin">
                  登录
                </el-button>
              </el-form-item>
            </el-form>
          </aside>

        </section>
      </article>
      <footer>
        <p>本网站版权归***所有</p>
      </footer>
    </div>
  </div>
</template>

<script>
  import axios from 'axios';

  export default {
    name: 'login',
    data() {
      const validateName = (rule, value, callback) => {
        if (value.length < 1) {
          callback(new Error('请输入正确的用户名称'));
        } else {
          callback();
        }
      };
      const validatePass = (rule, value, callback) => {
        if (value.length < 2) {
          callback(new Error('密码不能小于2位'));
        } else {
          callback();
        }
      };
      return {
        loginForm: {
          username: '',
          password: ''
        },
        loginRules: {
          username: [
            {required: true, trigger: 'blur', validator: validateName}
          ],
          password: [
            {required: true, trigger: 'blur', validator: validatePass}
          ]
        },
        loading: false,
        showDialog: false,
        username_i:"username_i",
        password_i:"password_i"
      }
    },
    created(){
      this.keyupSubmit();
    },
    methods: {
      usernameBlur(){
        this.username_i = "username_i";
      },
      usernameFocus(){
        this.username_i = "username_i_active";
      },
      passwordBlur(){
        this.password_i = "password_i";
      },
      passwordFocus(){
        this.password_i = "password_i_active";
      },
      handleLogin() {
        let that = this;
        // 加密用户名密码
        var  suff = new Date().getMilliseconds();
        let username = this.aesEncode(this.loginForm.username) + "_" + suff;
        let password = this.aesEncode(this.loginForm.password) + "_" + suff;
        var req = {
          username:username,
          password:password,
          client_id:"cloud",
          client_secret:"cloud",
          grant_type:"password"
        }
        axios({
          method: 'post',
          url:config.loginUrl.tokenUrl,
          data:$.param(req),
          headers:{
            'Content-Type':'application/x-www-form-urlencoded'}
        })
          .then(function (response) {
            if(response.status == 200){
              var data = response.data;
              localStorage.setExpire('access_token',data.access_token,sysTimeOut);
              localStorage.setExpire("token_type",data.token_type,sysTimeOut)
              that.getUserInfo();
            }
          })
          .catch(function (error) {
            console.log(error);
            that.$message({
              message: error.response.data.message,
              type: 'error'
            });
          });
      },
      getUserInfo(){
        var access_token = localStorage.getExpire("access_token");
        axios.get(config.loginUrl.userInfo,{
          params: {
            access_token: access_token
          }
        })
          .then(function (response) {
            if(response.status == 200 &&response.data.message == "success"){
              localStorage.setExpire("userInfo",JSON.stringify(response.data.data),sysTimeOut)
              window.location.href= homePageUrl;
            }
          })
          .catch(function (error) {
            console.log(error);
          });
      },
      created() {
        // window.addEventListener('hashchange', this.afterQRScan);
      },
      destroyed() {
        // window.removeEventListener('hashchange', this.afterQRScan);
      },
      keyupSubmit(){
        document.onkeydown=e=>{
          let _key=window.event.keyCode;
          if(_key===13){
            this.handleLogin()
          }
        }
      },
      aesEncode(str) {
        var key = CryptoJS.enc.Utf8.parse("cBssbHB3ZA==HKXT");
        var encryptedData = CryptoJS.AES.encrypt(str, key, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
        return encryptedData.toString();
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login-container{
  .login-form{
    input{
      padding-left: 50px;
      height: 40px;
      border: 1px solid #47414133;
      margin-top: 20px;
      border-radius: 10px;
      background-color: rgba(255, 255, 255, 0.82);
      outline: none;
    }
    button{
      background-color: #00a0e9;
      border: none;
      width: 100%;
      height: 40px;
      letter-spacing: 3px;
      font-size: 20px;
      border-radius: 10px;
      color: #fff;
      outline: none;
    }
    .username_i{
      background-image:url("../images/login_sys/user1.png");
    }
    .username_i_active{
      background-image:url("../images/login_sys/user2.png");
    }
    .password_i{
      background-image:url("../images/login_sys/password1.png");
    }
    .password_i_active{
      background-image:url("../images/login_sys/password2.png");
    }
  }
  header, footer, section {
    width: 1300px;
    margin: 0 auto;
  }
  /*头部样式*/
  header em{
    font-size: 50px;
    height: 100px;
    line-height: 100px;
  }
  header span{
    font-size: 22px;
    margin-left:20px;
  }

  article{
    width: 100%;
    height: 600px;
    background-color: #00a0e9;
  }
  section{
    height: 600px;
    background-image: url("../images/login_sys/bg.png");
    background-size: auto 100%;
    background-repeat: no-repeat;
    position: relative;
    width: 1300px;
    margin: 0 auto;
  }
  /*表单的样式*/
  aside{
    width: 400px;
    height: 450px;
    position: absolute;
    top: 70px;
    right: 100px;
    background-color:#fff9;
    border-radius: 15px;
  }
  aside em{
    display: block;
    width: 60px;
    height: 60px;
    margin: 25px auto 10px auto;
  }
  aside em img{
    width: 100%;
    height: auto;
  }
  form {
    width: 300px;
    margin: 0 auto;
  }
  form p{
    position: relative;
    margin-top: 10px;
  }
  form input{
    padding-left: 50px;
    width: 250px;
    height: 40px;
    border:1px solid #47414133;
    margin-top: 20px;
    border-radius: 10px;
    background-color:rgba(255, 255, 255, 0.82);
    outline: none;
  }
  form i{
    width: 30px;
    height: 30px;
    display: inline-block;
    position: absolute;
  }
  .name i{
    background-image: url("../images/login_sys/user1.png");
    background-repeat: no-repeat;
    left: 10px;
    top: 7px;
    z-index:99;
  }
  .password i{
    background-image: url("../images/login_sys/password1.png");
    background-repeat: no-repeat;
    left: 10px;
    top: 7px;
    z-index:99;
  }
  button{
    background-color:#00a0e9;
    border:none;
    width: 100%;
    height: 40px;
    letter-spacing: 3px;
    font-size: 20px;
    border-radius: 10px;
    color: #fff;
    outline: none;
  }
  .remember{
    width: 170px;
  }
  .remember input{
    width: 24px;
    height: 24px;
    background-image: url("../images/login_sys/rememberPassword1.png");
    -webkit-appearance: none;
    -moz-appearance: none;
    -o-appearance:none;
    padding-left: 0px;
    border-radius: 0px;
    background-color: rgba(0,0,0,0);
    border:none;
    vertical-align: middle;
    margin-right: 5px;
    margin-top: 0px;
  }
  .remember input:checked{
    /*background-image: url("../images/login_sys/rememberPassword2.png");*/
  }
  .remember,.regist{
    float: left;
    font-size: 15px;
    height: 40px;
    line-height: 40px;
    margin-top: 20px;
  }
  .regist{
    width: 130px;
  }
  .regist a{
    color: red;
  }
  .el-input__inner{
    padding: 0 0 0 40px;
    border-radius: 10px;
  }
  /*尾部样式*/
  footer{
    margin-top: 15px;
  }
  footer ul{
    width: 68%;
    margin: 0 auto;
  }
  footer ul li{
    width: 120px;
    text-align: center;
    font-size: 13px;
    height: 15px;
    line-height: 15px;
  }
  footer p{
    text-align: center;
    font-size: 13px;
    margin-top: 10px;
  }
  .el-form{
    .el-form-item{
      margin-top:30px;
      .el-form-item__content{
        margin-top:5px;
      }
    }
    .login-btn{
       margin-top:35px;
    }
  }
}
</style>
<style rel="stylesheet/scss" lang="scss" >
  .login-container{
    .el-input__inner{
      padding: 0 0 0 50px;
      border-radius: 10px;
    }
  }
</style>
